// taskStorage.cpp

#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include "TaskStorage.h"
#include "Task.h"


const std::string TaskStorage:: _fileName = "CalenizerStorage.txt";	

TaskStorage::TaskStorage() {
}

bool TaskStorage::loadFile(std::vector<Task> &taskList) {
	_fileInput.open(_fileName.c_str(), std::fstream::app);

	_fileInput.peek();
	if(_fileInput.eof()) {
		return false;
	} else { 
		while(_fileInput.is_open() && !_fileInput.eof()) {
		std::string nextLine;
		std::getline(_fileInput, nextLine);
		std::istringstream input(nextLine);
		Task newTask;
		newTask.convertStringToTask(input);
		taskList.push_back(newTask);
		_fileInput.peek();
	}
	}
	_fileInput.close();
	return true;
}


bool TaskStorage::writeFile(std::vector<Task> taskList) {
//	updateTaskList(contents);
	writeTaskList(taskList);
	return true;
}

void TaskStorage::writeTaskList(std::vector<Task> taskList) {
	_fileOutput.open(_fileName.c_str(), std::fstream::trunc);
	for(unsigned int i = 1; i <= taskList.size(); i++) {
		_fileOutput << taskList[i-1].convertToString();
	}
	_fileOutput.close();
}

/*
std::vector<Task> TaskStorage::getTaskList() {
	return _taskList;
}

void TaskStorage::updateTaskList(std::vector<Task> updatedTaskList) {
	_taskList = updatedTaskList;
}
*/